package habitcoach.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;

public class MainTab extends TabActivity
{
	private TabHost mTabHost;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mTabHost = getTabHost();

		setUpTabs();
		loadHabitList();

	}

	/**
	 * 
	 */
	private void setUpTabs()
	{
		mTabHost = this.getTabHost();// 从TabActivity上面获取放置Tab的TabHost
		LayoutInflater.from(this).inflate(R.layout.main_tab,
				mTabHost.getTabContentView(), true);

		mTabHost.addTab(mTabHost.newTabSpec("Add").setIndicator("Add",
				getResources().getDrawable(R.drawable.add)).setContent(
				R.id.widget_layout_add_habit));

		mTabHost.addTab(mTabHost.newTabSpec("Today").setIndicator("Today",
				getResources().getDrawable(R.drawable.today)).setContent(
				R.id.widget_layout_daily_list));

		mTabHost.addTab(mTabHost.newTabSpec("Review").setIndicator("Review",
				getResources().getDrawable(R.drawable.review)).setContent(
				R.id.widget_layout_review_list));

		mTabHost.addTab(mTabHost.newTabSpec("Setting").setIndicator("Setting",
				getResources().getDrawable(R.drawable.settings)).setContent(
				R.id.widget_layout_setting));

		mTabHost.setCurrentTab(1);
	}

	/**
	 * 
	 */
	private void loadHabitList()
	{
		ListView list = (ListView) findViewById(R.id.widget_layout_daily_list_view);

		// 生成动态数组，加入数据
		ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 10; i++)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();
			// map.put("ItemImage", R.drawable.checked);// 图像资源的ID
			map.put("ItemTitle", "Habit " + (i + 1));
			map.put("ItemText", "Exercise In The Morning. ");
			listItem.add(map);
		}
		// 生成适配器的Item和动态数组对应的元素
		SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
				R.layout.list_habits,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[]
				{ "ItemImage", "ItemTitle", "ItemText" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[]
				{ R.id.ItemImage, R.id.ItemTitle, R.id.ItemText });

		// 添加并且显示
		list.setAdapter(listItemAdapter);
	}
}
